{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb1db478-5ca3-4c6d-b09d-19258dbc8718",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import datetime as dt\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.colors as colors\n",
    "import colorsys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f7a26c0",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Read csv file into pandas dataframe\n",
    "filename = 'egads_sample_input_full'\n",
    "df = pd.read_csv('csv_files/' + filename + '.csv', sep = ',')\n",
    "df['date']=(pd.to_datetime(df['date'],unit='s')) # assume unix timestamp from input always in sec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70d3116a",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "df.head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f7c78cf",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Helper function make color (hex string/color string) lighter by multiplying luminosity and luminance_scale\n",
    "def lighten(color, luminance_scale):\n",
    "    try:\n",
    "        c = colors.cnames[color]\n",
    "    except:\n",
    "        c = color\n",
    "    h, l, s = colorsys.rgb_to_hls(*colors.to_rgb(c))\n",
    "    return colorsys.hls_to_rgb(h, 1 - luminance_scale * (1 - l), s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04e6eef3",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "output_folder = 'plots'\n",
    "fig = plt.figure(figsize=(35,10))\n",
    "ax = fig.add_subplot(111)\n",
    "\n",
    "# Plot original timeseries/prophet expected timeseries/egads expected timeseries\n",
    "ax.plot(df.date, df.original, label='Original Value', color='tomato')\n",
    "ax.plot(df.date, df.prophetExpected, label='Prophet Predicted', color=lighten('b',0.1))\n",
    "ax.plot(df.date, df.egadsExpected, label='Egads Predicted', color=lighten('r',0.1))\n",
    "       \n",
    "# Plot anomalies\n",
    "both_anomaly_flag = False\n",
    "prophet_anomaly_flag = False\n",
    "egads_anomaly_flag = False\n",
    "shared_cnt = 0\n",
    "for idx, row in df.iterrows():\n",
    "    if row['prophetAnomaly'] == 'yes' and row['egadsAnomaly'] == 'yes':\n",
    "        shared_cnt += 1\n",
    "        if not both_anomaly_flag:\n",
    "            ax.scatter(row['date'], row['original'], color='red', label=\"Both Prophet/Egads anomaly\")\n",
    "            both_anomaly_flag = True\n",
    "        else:\n",
    "            ax.scatter(row['date'], row['original'], color='red')\n",
    "    elif row['prophetAnomaly'] == 'yes':\n",
    "        if not prophet_anomaly_flag:\n",
    "            ax.scatter(row['date'], row['original'], color='blue', label=\"Prophet anomaly\")\n",
    "            prophet_anomaly_flag = True\n",
    "        else:\n",
    "            ax.scatter(row['date'], row['original'], color='blue')\n",
    "    elif row['egadsAnomaly'] == 'yes':\n",
    "        if not egads_anomaly_flag:\n",
    "            ax.scatter(row['date'], row['original'], color='green', label=\"Egads anomaly\")       \n",
    "            egads_anomaly_flag = True\n",
    "        else:\n",
    "            ax.scatter(row['date'], row['original'], color='green')              \n",
    "ax.legend(loc = 'best')\n",
    "plt.title(\"Visualize \" + filename + '.csv results')\n",
    "plt.savefig(output_folder + '/' + filename + '.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d91ff4b-8cd3-4124-8c95-9346bce18ef2",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# count # of anomalies\n",
    "print(\"# of Prophet anomalies\", (df.prophetAnomaly == \"yes\").sum())\n",
    "print(\"# of Egads anomalies\", (df.egadsAnomaly == \"yes\").sum())\n",
    "print(\"# of shared anomalies\", shared_cnt)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
